package Reminder.view.components;
import Reminder.domain.Period;
import Reminder.domain.Schedule;
import com.alibaba.fastjson.JSONObject;
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;

import java.time.LocalTime;
import java.util.ArrayList;

public class ScheduleCell {

    public static VBox generatePeriodSpan(Schedule schedule) {
        VBox vBox = new VBox();
//        Schedule schedule = new Schedule(40,10, LocalTime.of(8,30),LocalTime.of(8,30),LocalTime.of(7,0),4,4,3);
        LocalTime monrning = schedule.getMorningFirstPeriodTime();
        LocalTime afternoon = schedule.getAfternoonFirstPeriodTime();
        LocalTime evening = schedule.getEveningFirstPeriodTime();
        int periodSpan = schedule.getPeriodTimeSpan();
        int intervalSpan = schedule.getIntervalSpan();
        vBox.getChildren().add(generateHead(""));
        for (int i = 0; i < schedule.getMorningPeriodSum();i++){
            vBox.getChildren().add(generatePeriodSpanCell(monrning.plusMinutes((periodSpan+ intervalSpan)*i).toString(),monrning.plusMinutes(periodSpan *(i + 1) + intervalSpan*i).toString()));
        }
        for (int i = 0; i < schedule.getAfternoonPeriodSum();i++){
            vBox.getChildren().add(generatePeriodSpanCell(afternoon.plusMinutes((periodSpan+ intervalSpan)*i).toString(),afternoon.plusMinutes(periodSpan *(i + 1) + intervalSpan*i).toString()));
        }
        for (int i = 0; i < schedule.getEveningPeriodSum();i++){
            vBox.getChildren().add(generatePeriodSpanCell(evening.plusMinutes((periodSpan+ intervalSpan)*i).toString(),evening.plusMinutes(periodSpan *(i + 1) + intervalSpan*i).toString()));
        }


        return vBox;
    }

    public static VBox generatePeriodSpanCell(String startTime,String endTime) {
        VBox vBox = new VBox();
        vBox.setPrefSize(200,100);
        vBox.setAlignment(Pos.CENTER);
        Label startTimeLabel = new Label(startTime);
        Label divider = new Label("~");
        Label endTimeLabel = new Label(endTime);
        vBox.getChildren().add(startTimeLabel);
        vBox.getChildren().add(divider);
        vBox.getChildren().add(endTimeLabel);
        vBox.setStyle(
            "-fx-padding: 10;"+
            "-fx-border-style: solid inside;"+
            "-fx-border-width: 2;"+
            "-fx-border-insets: 5;"+
            "-fx-border-radius: 5;"+
            "-fx-border-color:green;"
        );
        return vBox;
    }

    public static VBox generateADaySchedule(String head,ArrayList<Period> periods) {
        VBox vBox = new VBox();
        vBox.getChildren().add(generateHead(head));
        for(Period period:periods){
            if(period.isNone()){
                vBox.getChildren().add(generatePeriodCell("","","","","grey"));
            }
            else{
                vBox.getChildren().add(generatePeriodCell(period.getCourse().getName(),period.getCourse().getTeacher(),period.getCourse().getClassroom(),period.getCourse().getDescription(),period.getCourse().getColor()));
            }
        }
        return vBox;
    }
    public static VBox generateHead(String day){
        VBox vBox = new VBox();
        vBox.setPrefSize(200,50);
        vBox.setAlignment(Pos.CENTER);
        Label label = new Label(day);
        vBox.getChildren().add(label);

        vBox.setStyle(
            "-fx-padding: 10;"+
            "-fx-border-style: solid inside;"+
            "-fx-border-width: 2;"+
            "-fx-border-insets: 5;"+
            "-fx-border-radius: 5;"+
            "-fx-border-color:green;"
        );
        return vBox;
    }
    public static VBox generatePeriodCell(String name, String teacher, String classroom, String description, String color){
        VBox vBox = new VBox();
        vBox.setPrefSize(200,100);
        vBox.setMinSize(200,100);
        vBox.setAlignment(Pos.CENTER);
        Label nameLabel = new Label(name);
        Label teacherLabel = new Label(teacher);
        Label classroomLabel = new Label(classroom);
        Label descriptionLabel = new Label(description);
        vBox.getChildren().add(nameLabel);
        vBox.getChildren().add(teacherLabel);
        vBox.getChildren().add(classroomLabel);
        vBox.getChildren().add(descriptionLabel);
        vBox.setStyle(
            "-fx-border-style: solid inside;"+
            "-fx-border-width: 2;"+
            "-fx-border-insets: 5;"+
            "-fx-border-radius: 5;"+
            "-fx-border-color:" + color + ";"
        );
        return vBox;
    }

}

